package com.wyj.servlet;

import com.wyj.db.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Enumeration;

/**
 * Created by WYJ on 2017/5/29.
 */
@WebServlet(name = "com.wyj.servlet.InsertServlet")
public class InsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = DBConnection.getConnection();

        String sql="INSERT INTO ";

        Enumeration paramNames = request.getParameterNames();
        String tableName=paramNames.nextElement().toString().trim();
        sql+=tableName+" VALUES(";
        String paramName = (String)paramNames.nextElement();
        String paramValue = new String(request.getParameter(paramName).getBytes("ISO-8859-1"),"UTF-8");
        sql+="'"+paramValue+"'";
        while(paramNames.hasMoreElements()) {
            paramName = (String)paramNames.nextElement();
            paramValue = new String(request.getParameter(paramName).getBytes("ISO-8859-1"),"UTF-8");
            sql+=",'"+paramValue+"'";
        }
        sql+=")";
        try {
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql);
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        response.sendRedirect("http://https://55300249.qcloud.la/JapariFriends/DBcourse/"+tableName+".jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
